package com.jdd.common.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jdd.modules.configcartype.entity.ConfigCarType;
import com.jdd.modules.configcartype.mapper.ConfigCarTypeMapper;
import com.jdd.modules.order.entity.ParkOrderInfo;
import com.jdd.modules.order.mapper.ParkOrderInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 组装算费日志工具类
 *
 * @author liuyaowen
 */

@Component
@Slf4j
public class PackageFeeLogUtils {
    @Resource
    private ParkOrderInfoMapper parkOrderInfoMapper;
    @Resource
    private ConfigCarTypeMapper configCarTypeMapper;

    /**
     * @param carEnterLogId 车辆类型Id
     * @return java.util.Map
     * @description: 根据入场记录查询订单信息, 查询支付总金额
     * @author liujunwei@jdd966.com
     * @date 2021/4/26 15:22
     */
    public Map getOrderInfo(String carEnterLogId) {
        Map map = new HashMap(3);
        //根据最近的入场记录id查询对应的支付订单
        QueryWrapper<ParkOrderInfo> orderWrapper = new QueryWrapper<>();
        orderWrapper.eq("park_log_id", carEnterLogId);
        orderWrapper.eq("order_status", "2");
        orderWrapper.orderByDesc("create_time");
        List<ParkOrderInfo> orderInfos = parkOrderInfoMapper.selectList(orderWrapper);
        // 已支付订单总费用
        int orderTotalFee = 0;
        //总优惠金额
        int totalDiscAmt = 0;
        for (ParkOrderInfo orderInfo : orderInfos) {
            int discAmt = getIntegerFromString(orderInfo.getTotalDiscAmt()) + getIntegerFromString(orderInfo.getCouponAmt());
            int fee = getIntegerFromString(orderInfo.getChargePaid()) + discAmt;
            orderTotalFee = orderTotalFee + fee;
            totalDiscAmt = totalDiscAmt + discAmt;
        }
        map.put("orderInfoList", orderInfos);
        map.put("paidFee", orderTotalFee);
        map.put("totalDiscAmt", totalDiscAmt);
        return map;
    }


    /**
     * 得到车辆类型id
     *
     * @param parkCode  车场编号
     * @param carNature 车辆性质
     * @return 车辆类型id
     * @date 2021-07-28
     * @author: liuyaowen
     */
    public String getCarTypeId(String parkCode, String carNature) {
        LambdaQueryWrapper<ConfigCarType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ConfigCarType::getParkCode, parkCode);
        List<ConfigCarType> list = configCarTypeMapper.selectList(queryWrapper);
        for (ConfigCarType configCarType : list) {
            List<String> carTypes = Arrays.asList(configCarType.getCarTypeNo().split(","));
            if (carTypes.contains(carNature)) {
                return configCarType.getId();
            }
        }
        return null;
    }


    private int getIntegerFromString(String str) {
        if (StringUtils.isBlank(str)) {
            return 0;
        }
        return Integer.valueOf(str);
    }
}
